/**
 * 收藏管理 Composable
 */

import type { Favorite, FavoriteCheckResponse } from '~/types'

/**
 * 使用收藏功能
 */
export const useFavorites = () => {
  const { $api } = useNuxtApp()

  /**
   * 获取收藏列表
   */
  const getFavorites = async (): Promise<Favorite[]> => {
    return await $api('/favorites')
  }

  /**
   * 添加收藏
   */
  const addFavorite = async (toolId: number): Promise<Favorite> => {
    return await $api(`/favorites/${toolId}`, { method: 'POST' })
  }

  /**
   * 取消收藏
   */
  const removeFavorite = async (toolId: number): Promise<void> => {
    return await $api(`/favorites/${toolId}`, { method: 'DELETE' })
  }

  /**
   * 检查是否已收藏
   */
  const checkFavorite = async (toolId: number): Promise<boolean> => {
    const response = await $api(`/favorites/check/${toolId}`) as FavoriteCheckResponse
    return response.isFavorited
  }

  return {
    getFavorites,
    addFavorite,
    removeFavorite,
    checkFavorite,
  }
}


